package com.cqc.designdemo02;

import android.app.ActivityOptions;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.SwitchCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private BottomNavigationView bottonNavigationView;
    private EditText et_phone;
    private EditText et_pwd;
    private Button btn_login;
    private TextInputLayout textInputLayout1;
    private TextInputLayout textInputLayout2;
    private Toast toast;
    private SearchView searchView;
    private Button btn_day;
    private Button btn_night;
    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toast = Toast.makeText(MainActivity.this, "", Toast.LENGTH_SHORT);
        findViews();
        initViews();
    }

    private void initViews() {
        initBottomNavigationItemView();//BottomNavigationItemView
        initEditTextAndButton();//TextInputLayout
        initSearchView();//SearchView
        initDayOrNight();//白天+夜间模式切换
        initImageView();//过度动画
    }

    private void initImageView() {
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //ActivityOptionsCompat必须放在点击事件中
                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, iv, "transition");
                startActivity(new Intent(MainActivity.this, NewActivity.class), optionsCompat.toBundle());
            }
        });
    }

    private void initDayOrNight() {
        btn_day.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                recreate();
            }
        });
        btn_night.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                recreate();
            }
        });
    }

    private void initSearchView() {
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            //点击软键盘输入法中的搜索按钮调用这个方法
            @Override
            public boolean onQueryTextSubmit(String query) {
                toast.setText(query);
                toast.show();
                return true;
            }

            //newText:搜索框内的整体值
            @Override
            public boolean onQueryTextChange(String newText) {
                toast.setText(newText);
                toast.show();
                return true;
            }
        });
    }

    private void initEditTextAndButton() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginApp();
            }
        });
        //设置软键盘的点击事件
        et_pwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) {
                if (id == R.id.login || id == EditorInfo.IME_ACTION_DONE) {
                    loginApp();
                    return true;//事件被消耗
                }
                return false;
            }
        });
    }

    private void loginApp() {
        //刚开始不显示错误信息，只有判断之后才会提示错误信息
        textInputLayout1.setErrorEnabled(false);
        textInputLayout2.setErrorEnabled(false);

        String phone = et_phone.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            textInputLayout1.setError("手机号不可为空");
        } else if (phone.length() != 11) {
            textInputLayout1.setError("请输入正确的手机号");
        } else if (TextUtils.isEmpty(pwd)) {
            textInputLayout2.setError("密码不可为空");
        } else if (pwd.length() < 6) {
            textInputLayout2.setError("密码不少于6位");
        } else {
            toast.setText("登录成功");
            toast.show();
        }
    }

    private void initBottomNavigationItemView() {
        bottonNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.tab1:
                        toast.setText("tab1");
                        toast.show();
                        break;
                    case R.id.tab2:
                        toast.setText("tab2");
                        toast.show();
                        break;
                    case R.id.tab3:
                        toast.setText("tab3");
                        toast.show();
                        break;
                    case R.id.tab4:
                        toast.setText("tab4");
                        toast.show();
                        break;
                    case R.id.tab5:
                        toast.setText("tab5");
                        toast.show();
                        break;
                }
                return true;
            }
        });
    }

    private void findViews() {
        bottonNavigationView = (BottomNavigationView) findViewById(R.id.bottomNavigationView);

        textInputLayout1 = (TextInputLayout) findViewById(R.id.textInputLayout1);
        textInputLayout2 = (TextInputLayout) findViewById(R.id.textInputLayout2);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_pwd = (EditText) findViewById(R.id.et_pwd);
        btn_login = (Button) findViewById(R.id.btn_login);

        searchView = (SearchView) findViewById(R.id.searchView);

        btn_day = (Button) findViewById(R.id.btn_day);
        btn_night = (Button) findViewById(R.id.btn_night);

        iv = (ImageView) findViewById(R.id.iv);
    }
}
